package view;

import java.util.Iterator;

import java.util.TreeSet;

import model.MediaProperty;
import model.MediaType;
import model.filemanager.*;

/*import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;*/
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;

public class MediaInfo {
	
	// Public Methods
	public static String GenerateForm(MediaFile file, String type, boolean editable){
		StringBuffer buffer = new StringBuffer();
		
		if(editable){
			buffer.append("<form onsubmit='return editSubmit()' id='editForm' action=\"/update?key="+file.getKey()+"\" method=\"post\" class=\"infoform\">");
		}else{
			buffer.append("<div class=\"infoform\">");
		}
		
		Iterator<MediaProperty> props = MediaType.getMediaType(type).getAllMediaProperties().iterator(); 
 		MediaProperty prop;
 		while(props.hasNext()){
 			prop = props.next(); 
 			if(prop.visible){
 				if(editable && prop.editable ){ // edit mode
 					buffer.append(String.format("<div><div class=\"formkey\">%s</div><div class=\"formvalue\">",prop.displayName)); // field desc
 					buffer.append(String.format("<input type=\"textarea\" name=\"%s\" value=\"%s\" rows=\"%s\" /></div></div>",prop.name,file!=null && file.getProperty(prop.name)!=null ? file.getProperty(prop.name) : "",3));
 				}else if(file!=null && file.getProperty(prop.name) != null && file.getProperty(prop.name) != ""){ // view mode
 					buffer.append(String.format("<div><div class=\"formkey\">%s</div><div class=\"formvalue\">",prop.displayName)); // field desc
 					buffer.append(MediaFormatter.FormatString(file, prop.name, prop.formatType)+"</div></div>");
 				}
 			}
 		}
		
 		if(editable){
			buffer.append("<div><input type=\"submit\" value=\"Submit\"></div>");
			buffer.append("</form>");
		}else{
			buffer.append("</div>");
		}
 		
 		return buffer.toString();
	}
	
	public String GenerateTable(String type, MediaFile[] files, String listName)
	{
		StringBuffer table = new StringBuffer();
		
		// Get list of properties for media type
		TreeSet mediaProps = (TreeSet) MediaType.getMediaType(type).getAllMediaProperties();
		Iterator<MediaProperty> props = mediaProps.iterator();
		
		table.append("<table class=\"contentTable\" id=\"mainContentTable\">");
		
		// Title row
		table.append("<tr id=\"tableHeader\">");
		
		table.append("<td>Click to View</td>"); // icons column
		
		MediaProperty prop;
		while(props.hasNext()){
 			prop = props.next();
 			
 			if(prop.visible) if(prop.sortable){
 				table.append(String.format("<td class=\"%s\"><a href=\"%s\">%s</a></td>",prop.name, "javascript:sortNavigate('"+prop.name+"')" ,prop.displayName));
 			}else{
 				table.append(String.format("<td>%s</td>", prop.displayName));
 			}
		}
		table.append("<td>Controls</td>");
		
		table.append("</tr>");
		
		// Rows for each media File
		for(MediaFile m: files)
		{		
			table.append(String.format("<tr class='tableRow' id=\"%s\">", "row"+m.getKey())); // Row tag begin
			
			// Show thumbnail / icon
			table.append(String.format("<td class=\"%s\">%s</td>","", MediaFormatter.FormatString(m, "", MediaFormatTypeEnum.IMG80_POPUP)));
			
			// Iterate though properties
			props = mediaProps.iterator();
			while(props.hasNext()){
	 			prop = props.next();
	 			
	 			if(prop.visible)
	 				table.append(String.format("<td class=\"%s\">%s</td>",prop.name, MediaFormatter.FormatString(m, prop.name, prop.formatType)));
			}
			
			// controls
			table.append("<td class='rowcontrols'>");
			table.append(String.format("<a href=\"javascript:showPopup('edit', '%s')\">"
					+"<img src=\"../images/information.gif\" alt=\"View File data\" title=\"Information\"></img></a>",m.getKey()));
			table.append(String.format("<a href=\"/downloadfile?download=yes&key=%s\">"
					+"<img src=\"../images/download.gif\" alt=\"Download File\" title=\"Download\"></img></a>",m.getKey()));
			//table.append(String.format("<a href=\"/deletefile?key=%s\">"
			table.append(String.format("<a href=\"javascript:deleteMediaFile('%s')\">"
					+"<img src=\"../images/delete.gif\" alt=\"Delete File\" title=\"Delete\"></img></a>",m.getKey()));
			if(listName != null && listName.length()!=0 && !listName.equals("-1")){
				table.append(String.format("<a href=\"javascript:removeMediaFromList('%s', '%s');\">"
					+"<img src=\"../images/remove.gif\" alt=\"Remove from List\" title=\"Remove\"></img></a></td>",listName,m.getKey()));
			}
			table.append("</td></tr>");
		}
		
		table.append("</table>");
		
		return table.toString();
		
	}
	
	public String GeneratePager(int totalRecords, int offset)
	{
		StringBuffer table = new StringBuffer();
		table.append("<table id=\"pagerTable\"><tr><td>");
		if (offset == 0)
		{
			table.append("&lt;&lt;");
		}
		else
		{
			table.append("<a class=\"reg_button\" href=\"javascript:pageNavigate('back')\">&lt;&lt;</a>");
		}
		int recordsShown = 10;
		if (totalRecords - offset < 10)
		{
			recordsShown = totalRecords - offset;
		}
		table.append(String.format("</td><td>%d - %d of %d</td><td>", offset, offset + recordsShown, totalRecords));
		
		
		if (totalRecords - offset <= 10)
		{
			table.append("&gt;&gt;");
		}
		else
		{
			table.append("<a class=\"reg_button\" href=\"javascript:pageNavigate('forward')\">&gt;&gt;</a>");
		}
		table.append("</td></tr></table>");
		return table.toString();
	}

	
}
